<?php

namespace app\widgets;

use app\models\User;
use Yii;
use yii\base\Widget;
use yii\helpers\Html;
use yii\helpers\Url;

class AppUserMenuWidget extends Widget {

    /* @var $user User */
    public $user;

    protected $userLabels = [
        User::TYPE_STUDENT => '个人资料',
        User::TYPE_ORG => '社会组织',
        User::TYPE_COMPANY => '企业资料'
    ];

    public function run() {
        $items = [
            ['label' => '待参加', 'url' => ['user/pre-attend']],
            ['label' => '|'],
            ['label' => '待面试', 'url' => ['user/pre-interview']],
            ['label' => '|'],
            ['label' => '我要发布', 'url' => ['publish/index']],
            ['label' => '|'],
            ['label' => '我的发布', 'url' => ['user/publish']],
            ['label' => '|'],
            ['label' => '报名者', 'url' => ['user/apply-list']],
            ['label' => '|'],
            ['label' => $this->userLabels[$this->user->type], 'url' => ['user/index']]
        ];
        if (!$this->user->getIsStudent()) {
            $items = array_slice($items, 4);
        } else {
            $items[5] = ['label' => '发布中心', 'url' => ['user/pub']];
            unset($items[4], $items[6], $items[8], $items[9]);
        }
        //print_r($items);
        $lines = [];
        foreach ($items as $item) {

            $activeClass = $this->isActive($item) ? 'on' : '';

            $text = Html::tag('div', $item['label']);
            $lines[] = isset($item['url']) ? Html::a($text, NULL, ['data-href' => Url::to($item['url']), 'class' => $activeClass]) : Html::tag('span', $item['label']);
        }

        return join("\n", $lines);
    }

    protected function isActive($item) {
        if (!isset($item['url'])) {
            return FALSE;
        }
        $route = trim(Yii::$app->controller->route, '/');
        return $route == $item['url'][0];
    }

}